package com.yuanxiao.common.core.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yuanxiao.common.core.pojo.MapLocation;
import com.yuanxiao.common.core.result.R;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import javax.json.Json;
import java.math.BigDecimal;

/**
 * 腾讯地图util
 * Author: cg
 * Date: 2023/2/26 16:22
 * Content:
 */
public class TxMapUtils {

    /**
     * 请求腾讯地图API计算起终点距离单位：米
     * @param fromLat：起点纬度
     * @param fromLng：起点经度
     * @param toLat：终点纬度
     * @param toLng：终点经度
     */
    public static R computeDistanceTxMap(String fromLat, String fromLng, String toLat, String toLng, String key) {
        String from = StrUtil.format("{},{}",fromLat,fromLng);
        String to = StrUtil.format("{},{}",toLat,toLng);
        String uri = StrUtil.format("https://apis.map.qq.com/ws/distance/v1/matrix?mode=driving&from={}&to={}&key={}",from,to,key);
        String distance = HttpURLConnectionToolsUtils.doGet(uri);
        if (StringUtils.isBlank(distance)){
            return R.error("起终点距离计算失败");
        }
        JSONObject distanceR = JSONUtil.parseObj(distance);
        if (NumberUtils.toInt(distanceR.get("status").toString()) != 0 || distanceR.get("result") == null){
            return R.error(distanceR.get("message").toString());
        }
        JSONObject resultMap = JSONUtil.parseObj(distanceR.get("result").toString());
        if (resultMap.get("rows") == null){
            return R.error("起终点距离计算失败");
        }
        JSONArray rows = JSONUtil.parseArray(resultMap.get("rows").toString());
        if (rows.size() <=0 ){
            return R.error("起终点距离计算失败");
        }
        JSONArray elements = JSONUtil.parseArray(JSONUtil.parseObj(rows.get(0)).get("elements"));
        if (elements.size() <=0 ){
            return R.error("起终点距离计算失败");
        }
        JSONObject distanceObj = JSONUtil.parseObj(elements.get(0));
        if (distanceObj.get("distance") == null){
            return R.error("起终点距离计算失败");
        }
        return R.returnDataWrapper(distanceObj.get("distance"));
    }

    /**
     * 你地址解析
     * @param address：地址
     */
    public static R addressResolution(String address, String key) {
        String uri = StrUtil.format("https://apis.map.qq.com/ws/geocoder/v1/?address={}&key={}",address,key);
        String result = HttpURLConnectionToolsUtils.doGet(uri);
        if (StringUtils.isBlank(result)){
            return R.error("地址解析错误");
        }
        JSONObject resultJson = JSONUtil.parseObj(result);
        if (NumberUtils.toInt(resultJson.get("status").toString()) != 0 || resultJson.get("result") == null){
            return R.error(resultJson.get("message").toString());
        }
        JSONObject resultMap = JSONUtil.parseObj(resultJson.get("result").toString());
        if (resultMap.get("location") == null){
            return R.error("地址解析错误");
        }
        JSONObject location = JSONUtil.parseObj(resultMap.get("location").toString());
        if (location.get("lat", BigDecimal.class) == null){// 纬度
            return R.error("地址解析错误");
        }
        if (location.get("lng", BigDecimal.class) == null){// 经度
            return R.error("地址解析错误");
        }
        MapLocation mapLocation = new MapLocation();
        mapLocation.setLat(location.get("lat", String.class));
        mapLocation.setLng(location.get("lng", String.class));
        return R.returnDataWrapper(mapLocation);
    }

    public static void main(String[] args) {
        String key = "UDNBZ-ZPGLJ-M4UFU-DKSF4-EWTKQ-MHFYT";
        System.out.println(JSONUtil.toJsonStr(TxMapUtils.addressResolution("云南省昆明市盘龙区万宏国际3期", key)));
    }
}
